Column

Mapa de la felicidad media

Column

Relacion felicidad y tiempo total de visualizacion de tv

Column

Relacion felicidad, salud y genero

---
title: "Untitled"
author: "Maria Isabel Gamez Salazar"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    social: menu
    source_code: embed 
---

```{r setup, include=FALSE}
library (ggplot2)
library (flexdashboard)
library (tidyverse)
library (maps)
library (DT)
library (shiny)
library (dplyr)
library (here)
library (rnaturalearth)
library (rnaturalearthdata)
```

```{r}
file_path <- "/Users/mariaisabelgamezsalazar/BDS2024/data/m8datoslimpios.csv"
m8datos_limpios <- read.csv(file_path)
```


Column {.sidebar data-width=300}
-----------------------------------------------------------------------
```{r}
selectInput("pais_sel", "Selecciona un país", choices = c('TODOS', sort(unique(m8datos_limpios$cntry))), selected='TODOS')
selectInput("year_sel", "Selecciona año", choices = c('TODOS', sort(unique(m8datos_limpios$year))), selected='TODOS')
selectInput("genero_sel", "Selecciona género", choices = c('TODOS', "1", "2"), selected='TODOS')
selectInput("health_sel", "Selecciona nivel de salud", choices = c('TODOS', sort(unique(m8datos_limpios$health))), selected='TODOS')

```

### Evolucion de la felicidad 2002-2014
Column {data-width=600}
```{r}
renderPlot({
  filtered_data <- m8datos_limpios
  if (input$pais_sel != 'TODOS') {
    filtered_data <- filtered_data %>% filter(cntry == input$pais_sel)
  }
  if (input$year_sel != 'TODOS') {
    filtered_data <- filtered_data %>% filter(year == as.numeric(input$year_sel))
  }
  if (input$genero_sel != 'TODOS') {
    filtered_data <- filtered_data %>% filter(gndr == as.numeric(input$genero_sel))
  }
  if (input$health_sel != 'TODOS') {
    filtered_data <- filtered_data %>% filter(health == as.numeric(input$health_sel))
  }
  
  avg_happy <- filtered_data %>%
    group_by(cntry, year) %>%
    summarize(mean_happy = mean(happy, na.rm = TRUE))
  
  custom_palette <- c("#999999", "#56B4E9", "#0072B2", "#E69F00", "#D55E00",
                      "#CC79A7", "#8B4513", "#B2DF8A", "#33A02C", "#FB9A99",
                      "#E31A1C", "#6A3D9A", "#FFFF99", "#F0E442", "#00CED1")

  ggplot(avg_happy, aes(x = year, y = mean_happy, color = cntry, group = cntry)) +
    geom_line(size = 1) +
    labs(title = "Promedio de Felicidad por País y Año",
         x = "Año",
         y = "Promedio de Puntuación de Felicidad",
         color = "País") +
    theme_minimal() +
    theme(axis.text.x = element_text(angle = 45, hjust = 1),
          legend.position = "bottom") +
    scale_color_manual(values = custom_palette)
})

```


```{r}
avg_happy <- m8datos_limpios %>%
  group_by(cntry, year) %>%
  summarize(mean_happy = mean(happy, na.rm = TRUE))

custom_palette <- c("#999999", "#56B4E9", "#0072B2", "#E69F00", "#D55E00",
                    "#CC79A7", "#8B4513", "#B2DF8A", "#33A02C", "#FB9A99",
                    "#E31A1C", "#6A3D9A", "#FFFF99", "#F0E442", "#00CED1")

ggplot(avg_happy, aes(x = year, y = mean_happy, color = cntry, group = cntry)) +
  geom_line(size = 1) +
  labs(title = "Promedio de Felicidad por País y Año",
       x = "Año",
       y = "Promedio de Puntuación de Felicidad",
       color = "País") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1),
        legend.position = "bottom") +
  scale_color_manual(values = custom_palette)
```
```

Column {data-width=50%}
-----------------------------------------------------------------------

### Mapa de la felicidad media

```{r}
media_felicidad <- m8datos_limpios %>%
  group_by(cntry) %>%
  summarise(mean_happy = mean(happy, na.rm = TRUE))

paises_seleccionados <- c("BE", "CH", "DE", "DK", "ES", "FI", "FR", "GB", "HU", "IE", "NL", "NO", "PL", "PT", "SE")
media_felicidad <- media_felicidad %>% filter(cntry %in% paises_seleccionados)

europe <- ne_countries(continent = "Europe", returnclass = "sf")

europe_selected <- europe %>% filter(iso_a2 %in% paises_seleccionados)

europe_happiness <- europe_selected %>%
  left_join(media_felicidad, by = c("iso_a2" = "cntry"))

mapa_felicidad <- ggplot(data = europe_happiness) +
  geom_sf(aes(fill = mean_happy)) +
  scale_fill_viridis_c(option = "plasma", name = "Media de Felicidad", na.value = "grey50") +
  labs(title = "Nivel de Felicidad en Europa (2002-2014)",
       caption = "Fuente: Encuesta ESS") +
  theme_minimal() +
  theme(
    plot.background = element_rect(fill = "white"),
    panel.background = element_rect(fill = "white"),
    axis.text = element_text(color = "black"),
    axis.title = element_text(color = "black"),
    plot.title = element_text(color = "black"),
    panel.grid.major = element_line(color = "grey", size = 0.5),
    panel.grid.minor = element_blank()
  )

```

Column {data-width=50%}
-----------------------------------------------------------------------

### Relacion felicidad y tiempo total de visualizacion de tv

```{r}

```
Column {data-width=50%}
-----------------------------------------------------------------------
### Relacion felicidad, salud y genero
```{r}


```